﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodtaskServices : Repository<corecmsfoodtask>, IcorecmsfoodtaskServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodtask entity)
        {
            return base.Change<corecmsfoodtask>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodtask> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodtask entity)
        {
            return base.Change<corecmsfoodtask>().Update(a => new corecmsfoodtask()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                taskTypeId = entity.taskTypeId,
                name = entity.name,
                addPriceWay = entity.addPriceWay,
                price = entity.price,
                currencyFlag = entity.currencyFlag,
                onLine = entity.onLine,
                isShare = entity.isShare,
                pinYin = entity.pinYin,
                updateTime = DateTime.Now
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodtask> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodtask>().Update(a => new corecmsfoodtask()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    taskTypeId = item.taskTypeId,
                    name = item.name,
                    addPriceWay = item.addPriceWay,
                    price = item.price,
                    currencyFlag = item.currencyFlag,
                    onLine = item.onLine,
                    isShare = item.isShare,
                    pinYin = item.pinYin,
                    updateTime = DateTime.Now
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodtask>().Update(a => new corecmsfoodtask()
            {
                isDelete = true,
                updateTime = DateTime.Now
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodtask>().Update(a => new corecmsfoodtask()
                {
                    isDelete = true,
                    updateTime = DateTime.Now
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodtask> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodtask> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, long taskTypeId, string sName, long storeId = 0)
        {
            List<corecmsfoodtask> lst = new List<corecmsfoodtask>();
            var where = Expressionable.Create<corecmsfoodtask>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.name.Contains(sName));
            }
            if (taskTypeId > 0)
                where = where.And(a => a.taskTypeId == taskTypeId);
            if (storeId > 0)
                where = where.And(a => a.storeId == storeId);

            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count).OrderBy(a => a.name).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.name).ToList();
            }

            return lst;
        }
        #endregion

        #region 根据点菜单Id获取商品做法
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodtask> QueryFromOrdering(long detailId, long foodId, long storeId = 0)
        {
            var foodTasks = new corecmsfooddetailtaskServices().GetList().Where(p => p.foodId == foodId && p.isDelete == false).ToList();

            List<corecmsfoodtask> lst = new List<corecmsfoodtask>();
            var where = Expressionable.Create<corecmsfoodtask>();
            where = where.And(a => a.isDelete == false);
            if (foodTasks.Count > 0)
            {
                var taskIds = foodTasks.Select(p => p.taskId).ToList();
                where = where.And(a => (a.storeId == storeId && a.currencyFlag == 1) || taskIds.Contains(a.id));
            }
            else
            {
                where = where.And(a => a.currencyFlag == 1 && a.storeId == storeId);
            }

            lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                .LeftJoin<corecmsfoodtaskcategory>((f, s) => f.taskTypeId == s.id)
                .Select((f, s) => new corecmsfoodtask
                {
                    id = f.id,
                    name = f.name,
                    taskTypeId = f.taskTypeId,
                    tastTypeName = s.name,
                    orderingTastId = 0,
                    selectedFlag = false,
                    pinYin = f.pinYin,
                    price = f.price,
                    addPriceWay = f.addPriceWay,
                }).OrderBy(f => f.taskTypeId).ToList();

            if (detailId > 0)
            {
                var orderingTasks = new corecmsfoodorderingtaskServices().GetList().Where(p => p.detailId == detailId).ToList();

                if (orderingTasks.Count > 0)
                {
                    foreach (var item in lst)
                    {
                        var orderingTask = orderingTasks.Where(p => p.tastId == item.id).FirstOrDefault();
                        if (orderingTask != null)
                        {
                            item.orderingTastId = orderingTask.id;
                            item.selectedFlag = true;
                        }
                    }
                }
            }

            return lst;
        }
        #endregion

        #region 根据点菜单Id获取商品做法
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodtask> QueryForSelected(List<long> taskIds)
        {
            var list = base.AsQueryable().Where(p => taskIds.Contains(p.id)).ToList();
            foreach (var item in list)
            {
                item.selectedFlag = true;
            }
            return list;
        }
        #endregion
    }
}
